<?php

class Application_Model_Dao_EntrepotMapper extends Base_Mapper
{
	
	function __construct(){
		parent::__construct();
		$this->tableName='Application_Model_DbTable_Entrepot';
	}
   
	public function save(Application_Model_BusiInfo_Entrepot $entry)
	{
		$data = array(
			'address' => $entry->get_address(),
			'phone' => $entry->get_phone(),
			'name' => $entry->get_name(),
			'contact' => $entry->get_contact(),
		);

		$id = $entry->get_id();
		if ($id === null || $id === "") {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
   
	public function find($id)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		return $this->toModel($row);		
	}
   
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {	
			$entry = new Application_Model_BusiInfo_Entrepot($row->toArray());
			$entries[] = $entry;
		}
		return $entries;
	}
	
	private function toModel($record){
		$model = new Application_Model_BusiInfo_Entrepot(); 
		$model->set_id($record->id);
	    $model->set_address($record->address);
	    $model->set_phone($record->phone);
	    $model->set_name($record->name);
	    $model->set_contact($record->contact);
	    return $model;
		
	}
	
	public function delete($id) {
		$where = 'id = ' .  $id;
		$this->getDbTable()->delete($where);
	}
}

